package com.scanner.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.scanner.bean.ScanSetting;
import com.scanner.hardware.R;
import com.scanner.util.BaseUtils;


public class ChooseTabSetDialog extends DialogFragment {
    private ScanSetting scanSetting;
    public static final String TABTYPE1 = "close";
    public static final String TABTYPE2 = "ahead";
    public static final String TABTYPE3 = "behind";

    public static ChooseTabSetDialog getInstance(){
        ChooseTabSetDialog scanType = new ChooseTabSetDialog();
        Bundle bundle = new Bundle();
        scanType.setArguments(bundle);
        return scanType;
    }

    private int type = 0;//0:添加Tab,1:设置多解码模式

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if(arguments != null){
            type = arguments.getInt("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomProgressDialog);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return inflater.inflate(R.layout.dialog_choose_tabset_layout,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RadioGroup rg = view.findViewById(R.id.rg_tab_select);
        RadioButton rbClose = view.findViewById(R.id.rb_tab_close);
        RadioButton rbQian = view.findViewById(R.id.rb_tab_qian);
        RadioButton rbHou = view.findViewById(R.id.rb_tab_hou);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnSure = view.findViewById(R.id.btn_update);
        scanSetting = BaseUtils.getSettingFormShare();
        if(TABTYPE1.equalsIgnoreCase(scanSetting.getTabChoose())){//默认打开服务一直开启
            rbClose.setChecked(true);
        }else if(TABTYPE2.equalsIgnoreCase(scanSetting.getTabChoose())){//点击按钮才打开扫描
            rbQian.setChecked(true);
        }else if(TABTYPE3.equalsIgnoreCase(scanSetting.getTabChoose())){
            rbHou.setChecked(true);
        }else {
            rbClose.setChecked(true);
        }

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.rb_tab_close){
                    scanSetting.setTabChoose(TABTYPE1);
                }else if(checkedId == R.id.rb_tab_qian){
                    scanSetting.setTabChoose(TABTYPE2);
                }else if(checkedId == R.id.rb_tab_hou){
                    scanSetting.setTabChoose(TABTYPE3);
                }
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissDialog();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtils.beanToString(scanSetting);
                dissDialog();
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        Window dialogWindow = getDialog().getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);

    }

    public void dissDialog(){
        if(getDialog()!= null && getDialog().isShowing()){
            getDialog().dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



}
